<?php
// +----------------------------------------------------------------------
// | Created by PhpStorm.
// +----------------------------------------------------------------------
// | Date: 2019/1/24 16:32
// +----------------------------------------------------------------------
// | Author: 曹成
// +----------------------------------------------------------------------

namespace app\api\controller;

use think\Controller;
use Config;

class Base extends Controller
{
    public $sign;
    public $private_key;
    public $apiproductversion;

    /**
     * 初始化
     */
    public function initialize()
    {
        //资源服标识
        $this->sign = config('global.sign');
        //资源服私钥
        $this->private_key = config('global.private_key');
        //产品api接口验证版本号
        $this->apiproductversion = config('global.apiproductversion');
    }

    /**
     * 服务器验证
     * @param string $verify
     * @return array
     */
    protected function serviceVerify($verify = '')
    {
        $data = json_decode(decoding($verify, $this->private_key), true);
        if (!isset($data['user_id']) || !isset($data['guid']) || !isset($data['sign']) || !isset($data['apiproductversion']) || !isset($data['date'])) {
            return $this->returnArr('服务器验证失败！');
        }
        if ($data['sign'] != $this->sign || $data['apiproductversion'] != $this->apiproductversion || $data['date'] != date('Ymd', time())) {
            return $this->returnArr('服务器验证失败！');
        }
        return $this->returnArr('', true, $data);
    }

    /**
     * 信息返回
     * @param string $msg
     * @param bool $state
     * @param array $data
     * @return array
     */
    protected function returnArr($msg = '', $state = false, $data = [])
    {
        return ['state' => $state, 'msg' => $msg, 'data' => $data];
    }

    /**
     * 返回json
     * @param string $msg
     * @param bool $state
     * @param array $data
     */
    public function returnJson($msg = '', $state = false, $data = [])
    {
        echo json_encode(['state' => $state, 'msg' => $msg, 'data' => $data]);
        exit;
    }


}